/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2022-2022. All rights reserved.
 */

import { expect } from '@jest/globals';
import * as fse from 'fs-extra';
import * as path from 'path';

import { initByStageEmptyApp, initByStageEmptyService, initExecuteCommand, runHvigor } from './common/common';
import { ModuleBuildProfileModifier } from './common/modifier/module-build-profile-modifier';

describe('PackageHar test', () => {
  test.concurrent('test stage app package har', async () => {
    await initByStageEmptyApp(async (projectRoot) => {
      const commands = initExecuteCommand({
        mode: 'module',
        product: 'default',
        module: 'library@default',
        tasks: 'default@PackageHar'
      });
      const result = runHvigor(projectRoot, commands);
      expect(result.stdout.includes('Finished :library:default@PackageHar')).toBeTruthy();
      expect(result.stdout.includes('BUILD SUCCESSFUL')).toBeTruthy();
      const result2 = runHvigor(projectRoot, commands);
      expect(result2.stdout.includes('UP-TO-DATE :library:default@PackageHar')).toBeTruthy();
      expect(result2.stdout.includes('BUILD SUCCESSFUL')).toBeTruthy();
      const output = path.resolve(projectRoot, 'library/build/default/outputs');
      fse.removeSync(output);
      const result3 = runHvigor(projectRoot, commands);
      expect(result3.stdout.includes('Finished :library:default@PackageHar')).toBeTruthy();
      expect(result3.stdout.includes('BUILD SUCCESSFUL')).toBeTruthy();
    });
  });

  test.concurrent('test stage app package ohpm har', async () => {
    await initByStageEmptyApp(async (projectRoot) => {
      fse.writeFileSync(path.resolve(projectRoot, 'library', '.ohpmignore'), '**/*.json');

      const commands = initExecuteCommand({
        mode: 'module',
        product: 'default',
        module: 'library@default',
        tasks: 'default@PackageHar'
      });

      const result = runHvigor(projectRoot, commands);
      expect(result.stdout.includes('Finished :library:default@PackageHar')).toBeTruthy();
      expect(result.stdout.includes('BUILD SUCCESSFUL')).toBeTruthy();

      const output = path.resolve(projectRoot, 'library/build/default/outputs/default');
      expect(fse.readdirSync(output).indexOf('library.har')).not.toEqual(-1);

      const cacheResDir = path.resolve(projectRoot, 'library', 'build/default/cache/default/default@PackageHar/',
        'src/main/resources/en_US/element');
      expect(fse.readdirSync(cacheResDir).indexOf('string.json')).toEqual(-1);
    });
  });

  test.concurrent('test stage service package har', async () => {
    await initByStageEmptyService(async (projectRoot) => {
      new ModuleBuildProfileModifier(projectRoot, 'library').updateTargetRuntimeOS('default', 'OpenHarmony');
      const commands = initExecuteCommand({
        mode: 'module',
        product: 'default',
        module: 'library@default',
        tasks: 'default@PackageHar'
      });
      const result = runHvigor(projectRoot, commands);
      expect(result.stderr.includes('Atomic service development is not supported in the OpenHarmony stage model.'))
        .toBeTruthy();
      new ModuleBuildProfileModifier(projectRoot, 'library').updateTargetRuntimeOS('default', 'HarmonyOS');
      const modifyRuntimeOSResult = runHvigor(projectRoot, commands);
      expect(modifyRuntimeOSResult.stdout.includes('Finished :library:default@PackageHar')).toBeTruthy();
      expect(modifyRuntimeOSResult.stdout.includes('BUILD SUCCESSFUL')).toBeTruthy();
      const result2 = runHvigor(projectRoot, commands);
      expect(result2.stdout.includes('UP-TO-DATE :library:default@PackageHar')).toBeTruthy();
      expect(result2.stdout.includes('BUILD SUCCESSFUL')).toBeTruthy();
      const output = path.resolve(projectRoot, 'library/build/default/outputs');
      fse.removeSync(output);
      const result3 = runHvigor(projectRoot, commands);
      expect(result3.stdout.includes('Finished :library:default@PackageHar')).toBeTruthy();
      expect(result3.stdout.includes('BUILD SUCCESSFUL')).toBeTruthy();
    }, { sign: false });
  });

  test.concurrent('test stage app native package ohpm har', async () => {
    await initByStageEmptyApp(async (projectRoot) => {
      fse.writeFileSync(path.resolve(projectRoot, 'library', '.ohpmignore'), '**/*.json');

      const commands = initExecuteCommand({
        mode: 'module',
        product: 'default',
        module: 'library@default',
        tasks: 'default@PackageHar'
      });

      const result = runHvigor(projectRoot, commands);
      expect(result.stdout.includes('Finished :library:default@PackageHar')).toBeTruthy();
      expect(result.stdout.includes('BUILD SUCCESSFUL')).toBeTruthy();

      const output = path.resolve(projectRoot, 'library/build/default/outputs/default');
      expect(fse.readdirSync(output).indexOf('library.har')).not.toEqual(-1);

      const cacheResDir = path.resolve(projectRoot, 'library', 'build/default/cache/default/default@PackageHar/',
        'src/main/resources/en_US/element');
      expect(fse.readdirSync(cacheResDir).indexOf('string.json')).toEqual(-1);
      expect(fse.readdirSync(path.resolve(projectRoot, 'library', 'build/default/cache/default/default@PackageHar/src/main/cpp')))
        .toContain('types');
    });
  });
});
